package com.wanlian.router.ui.main.yun;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.smart.android.image.ImageLoader;
import com.wanlian.router.R;
import com.wanlian.router.bean.YunFile;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Hyu on 2018/9/8.
 * Email: fvaryu@163.com
 */
public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.PhotoHolder> {

    private List<YunFile> mStringList;

    public PhotoAdapter(List<YunFile> stringList) {
        mStringList = stringList;
    }

    @NonNull
    @Override
    public PhotoHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new PhotoHolder(LayoutInflater.from(viewGroup.getContext())
        .inflate(R.layout.layout_item_photo, viewGroup, false)
        );
    }

    @Override
    public void onBindViewHolder(@NonNull PhotoHolder photoHolder, int i) {
        YunFile yunFile = mStringList.get(i);
        ImageLoader.load(photoHolder.photo.getContext(), yunFile.getFilePath(), photoHolder.photo);

        photoHolder.itemView.setTag(yunFile);
        photoHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                YunFile yun = (YunFile) v.getTag();
                if (yun != null) {
                    PicturePreviewActivity.go(v.getContext(), yun);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mStringList.size();
    }

    class PhotoHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.image_logo)
        ImageView photo;

        public PhotoHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
